package com.restaurant.application.root.commonui.data;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;
import java.util.stream.Collectors;

public class TableData implements PickableBean {

    public static final String FN_TABLENAME = "tableName";
    public static final String FN_COLUMNS = "columns";
    public static final String FN_ROWS = "rows";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_TABLENAME,
            FN_COLUMNS,
            FN_ROWS);

    private String tableName;
    private List<ColumnData> columns;
    private List<RowData> rows;

    public TableData(String tableName, List<ColumnData> columns, List<RowData> rows) {
        setTableName(tableName);
        setColumns(columns);
        setRows(rows);
    }

    public TableData() {
    }

    public TableData duplicate() {
        TableData result = new TableData();
        result.tableName = this.tableName;
        result.columns = this.columns == null ? null : this.columns.stream().map(e6 -> e6 == null ? null : e6.duplicate()).collect(Collectors.toList());
        result.rows = this.rows == null ? null : this.rows.stream().map(e7 -> e7 == null ? null : e7.duplicate()).collect(Collectors.toList());
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String value) {
        this.tableName = value;
    }

    public TableData withTableName(String value) {
        setTableName(value);
        return this;
    }

    public TableData withTableName(String value, String defaultValue) {
        setTableName(value == null ? defaultValue : value);
        return this;
    }

    public List<ColumnData> getColumns() {
        if (columns == null) {
            columns = Lists.newArrayList();
        }
        return columns;
    }

    public void setColumns(List<ColumnData> value) {
        this.columns = value;
    }

    public TableData withColumns(List<ColumnData> value) {
        setColumns(value);
        return this;
    }

    public TableData withColumns(List<ColumnData> value, List<ColumnData> defaultValue) {
        setColumns(value == null ? defaultValue : value);
        return this;
    }

    public TableData addColumns(ColumnData value) {
        if (columns == null) {
            columns = Lists.newArrayList();
        }
        this.columns.add(value);
        return this;
    }

    public List<RowData> getRows() {
        if (rows == null) {
            rows = Lists.newArrayList();
        }
        return rows;
    }

    public void setRows(List<RowData> value) {
        this.rows = value;
    }

    public TableData withRows(List<RowData> value) {
        setRows(value);
        return this;
    }

    public TableData withRows(List<RowData> value, List<RowData> defaultValue) {
        setRows(value == null ? defaultValue : value);
        return this;
    }

    public TableData addRows(RowData value) {
        if (rows == null) {
            rows = Lists.newArrayList();
        }
        this.rows.add(value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "tableName":
                return getTableName();
            case "columns":
                return getColumns();
            case "rows":
                return getRows();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof TableData)) return false;
        return signature().equals(((TableData) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean TableData has no signature defined, no hash code or equals possible.");
    }
}
